/*
 * @Author: wwssaabb
 * @Date: 2021-08-14 17:04:06
 * @LastEditTime: 2021-08-15 22:51:03
 * @FilePath: \react_demo\cart_demo\src\app.jsx
 */
import React,{useState} from 'react';
import Cart from './components/Cart/Cart'
import './App.css'

const App = () => {
  const [data, setData] = useState({
    title:'购物车',
    list:[
      {id:'g01',name:'商品1',price:17,isChoose:false},
      {id:'g02',name:'商品2',price:56,isChoose:false},
      {id:'g03',name:'商品3',price:77,isChoose:false},
      {id:'g04',name:'商品4',price:48,isChoose:false},
      {id:'g05',name:'商品5',price:69,isChoose:false},
    ]
  })

  const setGoodState=(id,state)=>{
    setData(()=>{
      data.list.find(i=>i.id===id).isChoose=state
      return {title:'购物车',list:[...data.list]}
    })
  }

  const changeAll=(state)=>{
    console.log(state)
    setData(()=>{
      data.list.forEach(i=>i.isChoose=state)
      return {title:'购物车',list:[...data.list]}
    })
  }

  return (
    <div className='app'>
      <Cart {...data} setGoodState={setGoodState} changeAll={changeAll}></Cart>
    </div>
  );
}

export default App;
